/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TransactionsForm.java
 *
 * Created on Nov 1, 2011, 6:16:13 PM
 */
package introse;
import java.util.Date;
import java.awt.*; 
import java.sql.*;
import javax.swing.table.*;
/**
 *
 * @author jylland
 * 
 */import java.util.Date;
import javax.swing.JOptionPane;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import java.text.DecimalFormat;
public class TransactionsForm extends javax.swing.JFrame implements Runnable{
    private int quantity;
    Searchsupplier1 a=new Searchsupplier1();
     String e;
      boolean isSup=false;
    private Date date; 
   
     private String dataTrans[][] = {{}};
    private    String colTrans[] = {"ProductId","Name","Quantity","Amount",};
    private    DefaultTableModel modeltrans = new DefaultTableModel(dataTrans,colTrans);
    boolean running = true;
       String dataInv[][] = {{}};
        String colInv[] = {"ProductId","Name","SupplierId","Category","Selling Price","Number Of Stocks","Status"};
        DefaultTableModel modelInv = new DefaultTableModel(dataInv,colInv);
         Thread clockThread; 
    /** Creates new form TransactionsForm */
    public TransactionsForm() {
        initComponents();
        jTextField1.setEnabled(false);   
        jButton1.setEnabled(false);
        clockThread= new Thread(this); 
              // and let it start running 
        clockThread.start(); 
        Statement s=null;
        ResultSet rs=null;
     
        jTable2.setModel(modelInv);
        s=null;
        rs=null;
         try{
        Connect c = new Connect();
        Connection conn=c.dbConnect();
        s=conn.createStatement();
        String Sqls="SELECT * FROM Inventory WHERE Status='Active'";
        rs=s.executeQuery(Sqls);
        while(rs.next())
        {
         
          //rg.getString(column name);
         modelInv.insertRow(jTable2.getRowCount()-1,new Object[]{rs.getString("ProductId"),rs.getString("ProductName"),rs.getString("SupplierId"),rs.getString("Category"),rs.getFloat("SellingPrice"),rs.getInt("Quantity"),rs.getString("Status")});
           
        }
        conn.close();
        }catch(Exception es){
          System.out.println(es);
            
        }
        
          jTable1.setModel(modeltrans);    
    }
 public TransactionsForm(String eid) {
        initComponents();
        e=eid;
        jTextField1.setEnabled(false);   
        jButton1.setEnabled(false);
        clockThread= new Thread(this); 
              // and let it start running 
        clockThread.start(); 
        Statement s=null;
        ResultSet rs=null;
     
        jTable2.setModel(modelInv);
        s=null;
        rs=null;
         try{
        Connect c = new Connect();
        Connection conn=c.dbConnect();
        s=conn.createStatement();
        String Sqls="SELECT * FROM Inventory WHERE Status='Active'";
        rs=s.executeQuery(Sqls);
        while(rs.next())
        {
         
          //rg.getString(column name);
         modelInv.insertRow(jTable2.getRowCount()-1,new Object[]{rs.getString("ProductId"),rs.getString("ProductName"),rs.getString("SupplierId"),rs.getString("Category"),rs.getFloat("SellingPrice"),rs.getInt("Quantity"),rs.getString("Status")});
           
        }
        modelInv.removeRow(jTable2.getRowCount()-1);
        conn.close();
        }catch(Exception es){
          System.out.println(es);
            
        }
        
          jTable1.setModel(modeltrans);    
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jFrame1 = new javax.swing.JFrame();
        jLabel1 = new javax.swing.JLabel();
        jButton3 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jTextField1 = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jButton5 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jButton6 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        jComboBox1 = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        jCheckBox1 = new javax.swing.JCheckBox();
        jLabel4 = new javax.swing.JLabel();
        jButton14 = new javax.swing.JButton();
        jTextField2 = new javax.swing.JTextField();
        jButton24 = new javax.swing.JButton();

        javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
        jFrame1.getContentPane().setLayout(jFrame1Layout);
        jFrame1Layout.setHorizontalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jFrame1Layout.setVerticalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("New Transaction");
        setBounds(new java.awt.Rectangle(200, 100, 0, 0));
        setResizable(false);

        jLabel1.setFont(new java.awt.Font("Verdana", 0, 18));
        jLabel1.setText("New Transaction");

        jButton3.setFont(new java.awt.Font("Verdana", 0, 12));
        jButton3.setText("Check-out");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Verdana", 0, 12));
        jLabel2.setText("mm/dd/yy");

        jLabel3.setFont(new java.awt.Font("Verdana", 0, 12));
        jLabel3.setText("hh:mm");

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"100005","Safeguard", "19", "646"},
                {null, null,null, null},
                {null, null,null, null},
                {null, null,null, null}
            },
            new String [] {
                "Product ID","Product Name", "Quantity", "Amount"
            }
        ));
        jTable1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 398, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 282, Short.MAX_VALUE)
                .addGap(34, 34, 34))
        );

        jTextField1.setText("Enter Supplier ID ");
        jTextField1.setEnabled(false);
        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        jLabel8.setFont(new java.awt.Font("Verdana", 0, 12));
        jLabel8.setText("Supplier ID:");

        jLabel7.setText("Total Amount:");

        jLabel9.setText("0.00");

        jButton5.setFont(new java.awt.Font("Verdana", 0, 12));
        jButton5.setText("Cancel Transaction");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"1000001", "Safeguard", "1000002", "34", "100"},
                {"1000002", "Max", "1000003", "1", "200"},
                {"1000003", "Chippy", "1000002", "10", "50"},
                {"1000004", "Nova", "1000001", "26", "23"},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Product Id", "Name", "Supplier Id", "Selling Price", "Number of stocks"
            }
        ));
        jTable2.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane2.setViewportView(jTable2);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 509, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 283, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(33, Short.MAX_VALUE))
        );

        jButton6.setText(">>");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        jButton7.setText("<<");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "ProductID", "ProductName", "Category", "Quantity", "SellingPrice"}));

        jButton1.setFont(new java.awt.Font("Verdana", 0, 12));
        jButton1.setText("Search Supplier");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jCheckBox1.setFont(new java.awt.Font("Verdana", 0, 12));
        jCheckBox1.setText("Supplier Transaction");
        jCheckBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox1ActionPerformed(evt);
            }
        });

        jLabel4.setText("PHP");

        jButton14.setFont(new java.awt.Font("Verdana", 0, 12));
        jButton14.setText("Search");
        jButton14.setBorder(null);
        jButton14.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton14ActionPerformed(evt);
            }
        });

        jButton24.setFont(new java.awt.Font("Verdana", 0, 12));
        jButton24.setText("View All");
        jButton24.setBorder(null);
        jButton24.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton24ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(27, 27, 27)
                                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(jButton7)
                                            .addComponent(jButton6)))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(36, 36, 36)
                                        .addComponent(jCheckBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 176, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel7)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel4)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jButton14, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(jButton24, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(jLabel2)
                                                .addGap(50, 50, 50)
                                                .addComponent(jLabel3))
                                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                    .addComponent(jLabel1)))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(38, 38, 38)
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jButton14, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton24, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(114, 114, 114)
                        .addComponent(jButton6)
                        .addGap(18, 18, 18)
                        .addComponent(jButton7))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jCheckBox1)
                        .addGap(9, 9, 9)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel8)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel7)
                        .addComponent(jLabel4)
                        .addComponent(jLabel9)))
                .addGap(8, 8, 8)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    public void run() 
     { 
          // loop until told to stop 
          while (running) 
          { 
               // Construct the current date. 
               date = new Date(); 
               // Get the hours, minutes and hours 
               String mon=null;
               String day=null;
               if(date.getMonth()<10)
                   mon="0"+(date.getMonth()+1);
               else
                   mon=""+(date.getMonth()+1);
               if(date.getDay()<10)
                   day="0"+date.getDay();
               else
                   day=""+date.getDay();
               String month=date.getYear()+1900+"-"+mon+"-"+day;
               int minutes=date.getMinutes();
               String minue;
               if(date.getMinutes()<10)
                   minue="0"+minutes;
               else
                   minue=""+minutes;
               String time = date.getHours()+":"+minutes; 
      
               jLabel2.setText(month);
               jLabel3.setText(time); 
               jTextField1.setText(a.getSupid());
               //Now the reason for threads 
               try 
               { 
                    clockThread.sleep(500); 
               } 
               catch (InterruptedException e) 
               { 
                    System.out.println(e); 
                } 
             
          } 
     }
     public void destroy() 
         { 
              // will cause thread to stop looping 
              running = false; 
              // destroy it. 
              clockThread = null; 
         } 
          
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
  
}//GEN-LAST:event_jTextField1ActionPerformed

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed

    int response = JOptionPane.showConfirmDialog(null, "Are you sure you want cancel?", "Cancel Confirm", JOptionPane.YES_NO_OPTION);
    if (response == JOptionPane.YES_OPTION) {
         int rowcount=jTable1.getRowCount()-1;
         for(int i=0;i<rowcount;i++)
         {
             Statement s=null;
  ResultSet rs=null;
   s=null;
   rs=null;
     String ProductId=jTable1.getValueAt(0, 0).toString();
    int quantity=Integer.parseInt(jTable1.getValueAt(0,2).toString());
    int curquan=0; 
    try{
        Connect c = new Connect();
        Connection conn=c.dbConnect();
        s=conn.createStatement();
        String Sqls="SELECT * FROM Inventory WHERE ProductId='"+ProductId+"'";
        rs=s.executeQuery(Sqls);
         while(rs.next())
        {
         curquan=rs.getInt("Quantity");
        }
         if(isSup==false)
         Sqls="UPDATE Inventory SET Quantity='"+(curquan+quantity)+"' WHERE ProductId='"+ProductId+"'";
         else if(isSup==true)
         Sqls="UPDATE Inventory SET Quantity='"+(curquan-quantity)+"' WHERE ProductId='"+ProductId+"'";    
        s.executeUpdate(Sqls);
       
        conn.close();
        }catch(Exception es){
          System.out.println(es);
            
        }
           modeltrans.removeRow(0);
         }
        this.setVisible(false);
        new MainMenu(e).setVisible(true);
        
    } 
}//GEN-LAST:event_jButton5ActionPerformed

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
    a.setVisible(true);
    
}//GEN-LAST:event_jButton1ActionPerformed

private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
    boolean flag=false;
    Statement s=null;
        ResultSet rs=null;
        DecimalFormat twoDForm = new DecimalFormat("#0.00");
    String str1 = JOptionPane.showInputDialog(null, "Enter Quantity:", "", 1);
   
     int row[]=jTable2.getSelectedRows();
     String ProductId=jTable2.getValueAt(row[0],0 ).toString();
     double tamount=0;

    String ProductName=jTable2.getValueAt(row[0],1).toString();
    double sellprice=Float.parseFloat(jTable2.getValueAt(row[0],4).toString());
    int curquan=Integer.parseInt(jTable2.getValueAt(row[0],5).toString());
   
    try
    {
         quantity=Integer.parseInt(str1);
        
    }catch(Exception es)
    {
        flag=true;
        JOptionPane.showMessageDialog(null,"Invalid Input!","Input Error",JOptionPane.ERROR_MESSAGE);
        
    }
     double amount=sellprice*quantity;
    if(quantity>curquan&&isSup==false)
        JOptionPane.showMessageDialog (null, "Quantity entered is greater than current stock!", "", JOptionPane.ERROR_MESSAGE);
    else if(flag==false)
    {
       modeltrans.insertRow(jTable1.getRowCount()-1, new Object[]{ProductId,ProductName,quantity,twoDForm.format(amount)});
       int rowcount=jTable2.getRowCount()-1;
       for(int i=0;i<rowcount;i++)
       modelInv.removeRow(0);
       jTable2.setModel(modelInv);
        s=null;
        rs=null;
       
         try{
             
        String Sqls;
             Connect c = new Connect();
        Connection conn=c.dbConnect();
        s=conn.createStatement();
        if(isSup==false)
         Sqls="UPDATE Inventory SET Quantity='"+(curquan-quantity)+"' WHERE ProductId='"+ProductId+"'";
        else
        {
         Sqls="UPDATE Inventory SET Quantity='"+(curquan+quantity)+"' WHERE ProductId='"+ProductId+"'";    
        
        }
         s.executeUpdate(Sqls);
        Sqls="SELECT * FROM Inventory WHERE Status='Active'";
        rs=s.executeQuery(Sqls);
        while(rs.next())
        {
         
          //rg.getString(column name);
           // for(int i=0;i<count;i++){
           // if(rs.getString("ProductId")!=pId[i])
        modelInv.insertRow(jTable2.getRowCount()-1,new Object[]{rs.getString("ProductId"),rs.getString("ProductName"),rs.getString("SupplierId"),rs.getString("Category"),rs.getFloat("SellingPrice"),rs.getInt("Quantity"),rs.getString("Status")});
         
        //  }
        }
        conn.close();
        modelInv.removeRow(jTable2.getRowCount()-1);
        }catch(NumberFormatException x)
        {
                 JOptionPane.showMessageDialog (null, "Product is already in list!", "", JOptionPane.WARNING_MESSAGE);
        }
         catch(Exception es){
          System.out.println(es);
            
        }
         for(int i=0;i<jTable1.getRowCount()-1;i++)
         {
             tamount+=Float.parseFloat(jTable1.getValueAt(i,3).toString());
             
             
         }
         jLabel9.setText(twoDForm.format(tamount)+"");
              jCheckBox1.setEnabled(false);
    }
    
}//GEN-LAST:event_jButton6ActionPerformed

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
    String str1 = JOptionPane.showInputDialog(null, "Enter Payment:", "", 1);
    boolean flag=false;
    float input;
    input=0;
    try{
    input=Float.parseFloat(str1);
    }
    catch(Exception s)
    {
        JOptionPane.showMessageDialog(null,"Invalid payment!","Input Error",JOptionPane.ERROR_MESSAGE);
        flag=true;
    }
    String tn=null;
    int rowcount=jTable1.getRowCount()-1;
    float amount=Float.parseFloat(jLabel9.getText());
    if(amount>input)
      JOptionPane.showMessageDialog(null,"Insufficent payment!","",JOptionPane.ERROR_MESSAGE);
    else if(flag==false)
    {
   
     Statement s=null;
        ResultSet rs=null;
        
        String Sqls="INSERT INTO transaction(DateTrans,TimeTrans)VALUES('"+jLabel2.getText()+"','"+jLabel3.getText()+"')";
        try{
             Connect c = new Connect();
        Connection conn=c.dbConnect();
        s=conn.createStatement();
        s.executeUpdate(Sqls);
        Sqls="SELECT * FROM transaction where DateTrans='"+jLabel2.getText()+"' && TimeTrans='"+jLabel3.getText()+"'";
        rs=s.executeQuery(Sqls);
        while(rs.next())
        {
            tn=rs.getString("TransactionNUmber");
            
        }
        for(int i=0;i<rowcount;i++)
        { Sqls="INSERT INTO customertransaction(TransactionCode,ProductId,TransactionNo,Quantity,Amount)VALUES('"+tn+(jTable1.getValueAt(i, 0).toString())+"','"+jTable1.getValueAt(i, 0).toString()+"','"+tn+"','"+jTable1.getValueAt(i, 2).toString()+"','"+jTable1.getValueAt(i,3).toString() +"')";
        s.executeUpdate(Sqls);
        }
        JOptionPane.showMessageDialog(null,"Total Amount:"+amount+"\nChange:"+(input-amount),"",JOptionPane.INFORMATION_MESSAGE);
        conn.close();
        }catch(Exception es){
          System.out.println(es);
            
        }
        this.setVisible(false);
        new MainMenu(e).setVisible(true);
    }
}//GEN-LAST:event_jButton3ActionPerformed

private void jCheckBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox1ActionPerformed
// TODO add your handling code here:
       if (jCheckBox1.isSelected()==true){
   isSup=true;
    jButton1.setEnabled(true);
   }
   else{
           isSup=false;
    jTextField1.setEnabled(false);
    jButton1.setEnabled(false);
   }
     
}//GEN-LAST:event_jCheckBox1ActionPerformed

private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
 int row[]=jTable1.getSelectedRows();
 float tamount=0;
 DecimalFormat twoDForm = new DecimalFormat("#0.00");
  int rowcount=jTable2.getRowCount()-1;
 for(int i=0;i<rowcount;i++)
       modelInv.removeRow(0);
 String ProductId=jTable1.getValueAt(row[0], 0).toString();
 int quantity=Integer.parseInt(jTable1.getValueAt(row[0],2).toString());
 int curquan=0; 
 Statement s=null;
  ResultSet rs=null;
   s=null;
   rs=null;
    try{
        Connect c = new Connect();
        Connection conn=c.dbConnect();
        s=conn.createStatement();
         String Sqls="SELECT * FROM Inventory WHERE ProductId='"+ProductId+"'";
        rs=s.executeQuery(Sqls);
         while(rs.next())
        {
         curquan=rs.getInt("Quantity");
        }
         if(isSup==false)
         Sqls="UPDATE Inventory SET Quantity='"+(curquan+quantity)+"' WHERE ProductId='"+ProductId+"'";
         else if(isSup==true)
             Sqls="UPDATE Inventory SET Quantity='"+(curquan-quantity)+"' WHERE ProductId='"+ProductId+"'";
        s.executeUpdate(Sqls);
        Sqls="SELECT * FROM Inventory WHERE Status='Active'";
        rs=s.executeQuery(Sqls);
        while(rs.next())
        {
         
          //rg.getString(column name);
         modelInv.insertRow(jTable2.getRowCount()-1,new Object[]{rs.getString("ProductId"),rs.getString("ProductName"),rs.getString("SupplierId"),rs.getString("Category"),rs.getFloat("SellingPrice"),rs.getInt("Quantity"),rs.getString("Status")});
           
        }
        modelInv.removeRow(jTable2.getRowCount()-1);
        conn.close();
        }catch(Exception es){
          System.out.println(es);
            
        }
    modeltrans.removeRow(row[0]);
    try{
     for(int i=0;i<jTable1.getRowCount()-1;i++)
         {
             tamount+=Float.parseFloat(jTable1.getValueAt(i,3).toString());
 
         }
         jLabel9.setText(twoDForm.format(tamount)+"");
    }catch(Exception e)
    {
        jLabel9.setText("0.00");

    }
    if(tamount==0)
                jCheckBox1.setEnabled(true);
}//GEN-LAST:event_jButton7ActionPerformed

private void jButton14ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton14ActionPerformed
// TODO add your handling code here:
     String data[][] = {{}};
        String col[] = { "ProductId","Name","SupplierId","Category","Selling Price","Number Of Stocks"};
        DefaultTableModel model = new DefaultTableModel(data,col);
        jTable2.setModel(model);
        Statement s=null;
        ResultSet rs=null;
         try{
             Connect c = new Connect();
        Connection conn=c.dbConnect();
        s=conn.createStatement();
        String Sqls="SELECT * FROM Inventory WHERE "+jComboBox1.getSelectedItem().toString()+" LIKE '%"+jTextField2.getText()+"%'";
        rs=s.executeQuery(Sqls);
       while(rs.next())
       {
          //rg.getString(column name);
       model.insertRow(jTable2.getRowCount()-1,new Object[]{rs.getString("ProductId"),rs.getString("ProductName"),rs.getString("SupplierId"),rs.getString("Category"),rs.getString("SellingPrice"),rs.getString("Quantity"),rs.getString("Status")});
           
       }
        conn.close();
        }catch(Exception es){
          System.out.println(es);
            
        }
}//GEN-LAST:event_jButton14ActionPerformed

private void jButton24ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton24ActionPerformed
// TODO add your handling code here:
     Statement s=null;
        ResultSet rs=null;
     String dataInv[][] = {{}};
        String colInv[] = {"ProductId","Name","SupplierId","Category","Selling Price","Number Of Stocks","Status"};
        DefaultTableModel modelInv = new DefaultTableModel(dataInv,colInv);
        jTable2.setModel(modelInv);
        s=null;
        rs=null;
         try{
        Connect c = new Connect();
        Connection conn=c.dbConnect();
        s=conn.createStatement();
        String Sqls="SELECT * FROM Inventory";
        rs=s.executeQuery(Sqls);
        while(rs.next())
        {
         
          //rg.getString(column name);
         modelInv.insertRow(jTable2.getRowCount()-1,new Object[]{rs.getString("ProductId"),rs.getString("ProductName"),rs.getString("SupplierId"),rs.getString("Category"),rs.getString("SellingPrice"),rs.getString("Quantity"),rs.getString("Status")});
           
        }
        conn.close();
        }catch(Exception es){
          System.out.println(es);
            
        }
}//GEN-LAST:event_jButton24ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TransactionsForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TransactionsForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TransactionsForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TransactionsForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new TransactionsForm().setVisible(true);
                
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton14;
    private javax.swing.JButton jButton24;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JFrame jFrame1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration//GEN-END:variables
}
